package cn.jiyun.arrayList_1_22;

public class MyList {
    private static final int AA = 10;
    private int[] myList;
    private int size=0;//记录有效长度
//    private static MyList[] myList=new MyList[5];


    //不传参 默认10个长度
    public MyList() {
        this.myList=new int[AA];
    }

    //传参  传多少就是多少长度的数组
    public MyList(int num) {
        this.myList = new int[num];
    }

    //判断长度
    private void pan(){
        if ((size+1)>myList.length){
            //长度不够  需要扩容
            kuo();
        }
    }

    //扩容  扩容为2
    private void kuo(){
        int[] ints = new int[myList.length * 2];
        //将旧数组  中的元素放到新数组中
        for (int i = 0; i < myList.length; i++) {
            ints[i]=myList[i];
        }
        myList=ints;

    }

    //index是否合法
    private void index(int index){
        if (index<0 || index>=size){
            throw new RuntimeException("索引越界了");
        }
    }

    //添加
    public boolean add(int num){
        //判断数组是否够用
        pan();
        myList[size++]=num;
        return true;
    }

    //修改
    public boolean set(int index,int num){
        pan();
        myList[index]=num;
        return true;
    }

    //删除
    public int remove(int index){
        index(index);
        int remove = myList[index];
        for (int i = index; i < size-1; i++) {
            myList[index]=myList[index+1];
        }
        myList[--size]=0;
        return remove;
    }

    //查询
    public int get(int index){
        //判断 index 是否合法
        index(index);
        return myList[index];
    }

    //获取长度
    public int size(){
        return size;
    }


}